package com.sharainy.service.impl;


import com.sharainy.dao.AccountMapper;
import com.sharainy.dao.SettlementMapper;
import com.sharainy.pojo.Account;
import com.sharainy.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private SettlementMapper settlementMapper;

    @Override
    public List<Account> selectStartAccount(String merchantId) {
       List<Account> accountList = accountMapper.selectStartAccount(merchantId);
        if (accountList!=null) {
            List<Account> accounts = new ArrayList<Account>();
            String elecCollectors = "";//电能表计
            String waterCollectors = "";//水能表计
            String gasCollectors = "";//气能表计
            String elecReading = "";//电开户表底
            String waterReading = "";//水开户表底
            String gasReading = "";//气开户表底
            for (Account account : accountList) {
                if (account.getPowertype() == 0 ) {
                    elecCollectors += account.getCollectors() + ",";
                    elecReading += account.getBeforereading() + ",";
                } else if (account.getPowertype() == 1 ) {
                    waterCollectors += account.getCollectors() + ",";
                    waterReading += account.getBeforereading() + ",";
                } else if (account.getPowertype() == 2 ) {
                    gasCollectors += account.getCollectors() + ",";
                    gasReading += account.getBeforereading() + ",";
                }
            }
            if (!"".equals(elecCollectors)) {
                Account eclecAccount = new Account();
                eclecAccount.setName("电子账户");
                eclecAccount.setPowertype(0);
                eclecAccount.setCollectors(elecCollectors.substring(0, elecCollectors.length() - 1));
                eclecAccount.setReadingString(elecReading.substring(0, elecReading.length() - 1));
                accounts.add(eclecAccount);
            }
            if (!"".equals(waterCollectors)) {
                Account watercAccount = new Account();
                watercAccount.setName("水子账户");
                watercAccount.setPowertype(1);
                watercAccount.setCollectors(waterCollectors.substring(0, waterCollectors.length() - 1));
                watercAccount.setReadingString(waterReading.substring(0, waterReading.length() - 1));
                accounts.add(watercAccount);
            }
            if (!"".equals(gasCollectors)) {
                Account gasAccount = new Account();
                gasAccount.setName("气子账户");
                gasAccount.setPowertype(2);
                gasAccount.setCollectors(gasCollectors.substring(0, gasCollectors.length()- 1));
                gasAccount.setReadingString(gasReading.substring(0, gasReading.length()-1));
                accounts.add(gasAccount);
            }
            return accounts;
        }else{
            return null;
        }
    }

    @Override
    public List<Account> selectEndAccount(String merchantId) {
       List<Account> accountList = accountMapper.selectEndAccount(merchantId);
        if (accountList!=null) {
            List<Account> accounts = new ArrayList<Account>();
            String elecCollectors = "";//电能表计
            String waterCollectors = "";//水能表计
            String gasCollectors = "";//气能表计

            float elebeforereading = 0;
            float eleafterreading = 0;
            float elecost = 0;
            float eleusage = 0;
            float waterbeforereading = 0;
            float waterafterreading = 0;
            float watercost = 0;
            float waterusage = 0;
            float gasbeforereading = 0;
            float gasafterreading = 0;
            float gascost = 0;
            float gasusage = 0;
            for (Account account : accountList) {
                if (account.getPowertype() == 0 ) {
                    elecCollectors += account.getCollectors() + ",";
                    elebeforereading += account.getBeforereading();
                    eleafterreading += account.getNowreading();
                    elecost += account.getCost();
                    eleusage += account.getUsagepower();
                } else if (account.getPowertype() == 1 ) {
                    waterCollectors += account.getCollectors() + ",";
                    waterbeforereading += account.getBeforereading();
                    waterafterreading += account.getNowreading();
                    watercost += account.getCost();
                    waterusage += account.getUsagepower();
                } else if (account.getPowertype() == 2 ) {
                    gasCollectors += account.getCollectors() + ",";
                    gasbeforereading += account.getBeforereading();
                    gasafterreading += account.getNowreading();
                    gascost += account.getCost();
                    gasusage += account.getUsagepower();
                }
            }
            if (!"".equals(elecCollectors)) {
                Account eclecAccount = new Account();
                eclecAccount.setName("电子账户");
                eclecAccount.setPowertype(0);
                eclecAccount.setBeforereading(elebeforereading);
                eclecAccount.setNowreading(eleafterreading);
                eclecAccount.setCost(elecost);
                eclecAccount.setUsagepower(eleusage);
                accounts.add(eclecAccount);
            }
            if (!"".equals(waterCollectors)) {
                Account waterAccount = new Account();
                waterAccount.setName("水子账户");
                waterAccount.setPowertype(1);
                waterAccount.setBeforereading(waterbeforereading);
                waterAccount.setNowreading(waterafterreading);
                waterAccount.setCost(watercost);
                waterAccount.setUsagepower(waterusage);
                accounts.add(waterAccount);
            }
            if (!"".equals(gasCollectors)) {
                Account gasAccount = new Account();
                gasAccount.setName("气子账户");
                gasAccount.setPowertype(2);
                gasAccount.setBeforereading(gasbeforereading);
                gasAccount.setNowreading(gasafterreading);
                gasAccount.setCost(gascost);
                gasAccount.setUsagepower(gasusage);
                accounts.add(gasAccount);
            }
            return accounts;
        }else{
            return null;
        }
    }
}
